package com.zsrt.oa.employee.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zsrt.oa.employee.entity.EmployeeEntity;
import com.zsrt.oa.employee.service.EmployeeService;
import com.zsrt.oa.employee.service.impl.EmployeeServiceImpl;
import com.zsrt.utils.DBUtils;

public class EmployeeController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 实例化员工操作类
	 */
	private EmployeeService service = new EmployeeServiceImpl();
	
	/**
	 * 获取员工增加页面的数据
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		String type = request.getParameter("type");
		if(null != type && !"".equals(type.trim())){
			Map<String, Object> map = new HashMap<String, Object>();
			EmployeeEntity entity = new EmployeeEntity();
			if("add".equals(type)){
				String empPhone = request.getParameter("empPhone");
				String empPassword = request.getParameter("empPassword");
				String empSex = request.getParameter("empSex");
				String deptId = request.getParameter("deptId");
				String empRemarks = request.getParameter("empRemarks");
				String empHobbysArrays = request.getParameter("empHobbys");
				
				entity.setDeptId(deptId);
				entity.setEmpPhone(empPhone);
				entity.setEmpPassword(empPassword);
				entity.setEmpSex(empSex);
				entity.setEmpRemarks(empRemarks);
				
				//调用用户增加的方法
				service.add(entity, map,empHobbysArrays);
			}else if("query".equals(type)){
				String pageSize = request.getParameter("pageSize");
				String curPage = request.getParameter("curPage");
				
				//调用用户查询分页的方法
				service.query(pageSize, curPage, map);
			}else if("deptQuery".equals(type)){
				
				//调用部门动态生成的方法
				service.deptQuery(map);
			}else if("empHobby".equals(type)){
				
				//调用动态生成爱好的方法
				service.empHobby(map);
			}else if("delete".equals(type)){
				String empId = request.getParameter("empId");
				entity.setEmpId(empId);
				
				//调用用户指定id删除的方法
				service.empDeletebByIds(entity, map);
			}else if("findById".equals(type)){
				String empId = request.getParameter("empId");
				entity.setEmpId(empId);
				
				//调用根据id进行查找的方法
				service.empFindById(entity, map);
			}else if("login".equals(type)){
				String empPhone = request.getParameter("empPhone");
				String empPassword = request.getParameter("empPassword");
				
				//调用登录校验的方法
				service.login(empPhone,empPassword,map);
			}else if("leftMenuAppend".equals(type)){
				
				//调用动态菜单创建的方法
				service.leftMenuAppend(map);
			}else if("update".equals(type)){
				String empId = request.getParameter("empId");
				String empPhone = request.getParameter("empPhone");
				String empPassword = request.getParameter("empPassword");
				String empSex = request.getParameter("empSex");
				String deptId = request.getParameter("deptId");
				String empRemarks = request.getParameter("empRemarks");
				String empHobbysArrays = request.getParameter("empHobbys");
				
				entity.setEmpId(empId);
				entity.setDeptId(deptId);
				entity.setEmpPhone(empPhone);
				entity.setEmpPassword(empPassword);
				entity.setEmpSex(empSex);
				entity.setEmpRemarks(empRemarks);
				
				//调用用户修改的方法
				service.empUpdate(entity, map, empHobbysArrays);
			}
			
			DBUtils.toJson(map, response);
		}
	}
		
}
